<template>
  <div>
    <div class="tab-title-box">
      <span class="tab-item">个人资料</span>
    </div>
    <div class="tab-content-box">
      <el-form
        class="personal-el-form"
        ref="form"
        label-width="80px"
        size="mini"
        :model="formUser"
      >
        <h2 class="personal-base-header">基本信息</h2>
        <el-form-item label="头像">
          <el-upload
            action=""
            :show-file-list="false"
            :http-request="uploadHttp"
            :before-upload="beforeAvatarUpload"
            accept=".png, .jpg, .jpeg"
          >
            <el-avatar :size="100" :src="formUser.userHead || ''" fit="cover">
              <img
                alt=""
                src=""
              />
            </el-avatar>
          </el-upload>

          <!-- <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload> -->
        </el-form-item>

        <el-form-item label="昵称">
          <el-input
            maxlength="10"
            show-word-limit
            v-model="formUser.nickName"
          ></el-input>
        </el-form-item>

        <el-form-item label="邮箱">
          <el-input placeholder="请输入邮箱" v-model="formUser.userEmail">
          </el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input placeholder="请输入内容" v-model="formUser.userName">
          </el-input>
        </el-form-item>
        <el-form-item label="学号">
          <el-input
            placeholder="请输入内容"
            v-model="formUser.stuId"
            :disabled="true"
          >
          </el-input>
        </el-form-item>

        <el-form-item size="mini">
          <el-button type="primary" @click="onSubmit">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mapActions, mapState } from "vuex";
import oss from "@/api/oss";
import { profile } from "@/api/user";
export default {
  data() {
    return {
      formUser: {
        stuId: "",
        userName: "",
        nickName: "",
        userHead: "",
        userEmail: "",
      },
    };
  },
  computed: {
    ...mapState({ userInfo: (state) => state.user }),
  },
  mounted() {
    this.init();
  },
  methods: {
    ...mapActions("user", ["getInfo"]),
    init() {
      this.getInfo().then((res) => {
        this.formUser = res.item;
      });
    },
    /**
     * 图片限制
     */
    beforeAvatarUpload(file) {
      const isJPEG = file.name.split(".")[1] === "jpeg";
      const isJPG = file.name.split(".")[1] === "jpg";
      const isPNG = file.name.split(".")[1] === "png";
      const isLt500K = file.size / 1024 / 500 < 2;
      if (!isJPG && !isJPEG && !isPNG) {
        this.$message.error("上传图片只能是 JPEG/JPG/PNG 格式!");
      }
      if (!isLt500K) {
        this.$message.error("单张图片大小不能超过 500KB!");
      }
      return (isJPEG || isJPG || isPNG) && isLt500K;
    },
    onSubmit() {
      console.log(this.formUser);
      profile(this.formUser).then((res) => {
        if (res.code == 20000) {
          this.$message.success(res.message);
          this.init();
        }
      });
    },
    /**
     * 阿里云OSS上传
     */
    uploadHttp(param) {
      const formData = new FormData();
      console.log(param.file);
      formData.append("file", param.file);
      oss
        .uploadAvater(formData)
        .then((rsp) => {
          console.log("上传图片成功", rsp);
          if (rsp && rsp.success) {
            this.formUser.userHead = rsp.data.url;
          }
        })
        .catch((response) => {
          console.log("图片上传失败", response);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.tab-title-box {
  display: flex;
  height: 51px;
  line-height: 51px;
  border-bottom: 1px solid #e2e2e2;
  background: #ebeef5;

  .tab-item {
    margin-left: 40px;
    color: $theme-color-font;
    line-height: 51px;
    height: 52px;
    min-width: 65px;
    padding: 0 15px;
    text-align: center;
    cursor: pointer;
    font-size: 18px;
    float: left;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    border-top: 2px solid $theme-color-font;
    background: #fff;
  }
}
.tab-content-box {
  background: #fff;
  padding: 0 30px;
  .personal-base-header {
    font-size: 14px;
    font-weight: 400;
    padding: 0 15px;
    margin-bottom: 30px;
    margin-top: 20px;
  }
  .personal-el-form {
  }
}
</style>
